<template>
  <!-- 折线柱状图 -->
  <div class="echartBar" ref="right1"></div>
</template>

<script>
export default {
  components: {},
  props: ['chartData'],
  data () {
    return {}
  },
  computed: {},
  watch: {
    'chartData': {
      handler() {
        if (this.chartData.length > 0) {
          this.getEchartData();
        }
      },
      deep: true
    }
  },
  created () {},
  mounted() {
    this.getEchartData();
  },
  methods: {
    getEchartData() {
      let myChart = this.$echarts.init(this.$refs.right1);

      const xData = this.chartData.map(item => item.label);
      const yData1 = this.chartData.map(item => item.value1);
      const yData2 = this.chartData.map(item => item.value2);
      console.log('barLinedataX::',xData);
      console.log('barLinedataY1::',yData1);
      console.log('barLinedataY2::', yData2);

      let option = {
        grid: {
          top: "15%",
          left: "5%",
          right: "5%",
          bottom: "4%",
          containLabel: true,
        },
        legend: {
          top: '5%',
          left: 'center',
          itemGap: 90,
          // icon: 'circle', //  这个字段控制形状  类型包括 circle 圆形，triangle 三角形，diamond 四边形，arrow 变异三角形，none 无
          itemWidth: 20, // 设置图例图形的宽
          itemHeight: 10, // 设置图例图形的高
          textStyle: {
            fontFamily: 'Source Han Sans CN',
            fontWeight: 400,
            fontSize: 14,
            color: "#1D4A77",
          },
        },
        tooltip: {
          show: true,
          trigger: "axis",
          textStyle: {
            color: "#1D4A77",
          },
          axisPointer: {
            type: 'shadow',
          },
          backgroundColor: "rgba(255, 255, 255, 1)", //设置背景颜色
          borderColor: "transparent",
          confine: false,
          formatter: function (params) {
            var str = params[0].name;
            params.filter(function (item) {
              if(item.seriesName !=''){
                if (item.componentSubType == "bar") {
                  str += "<br/>" + item.seriesName + "：" + item.value + '人';
                }
                if (item.componentSubType == "line") {
                  str += "<br/>" + item.seriesName + "：" + item.value + '%';
                }
              }
            });
            return str;
          },
        },

        // dataZoom: [
        //   {
        //     show: xData.length > 7,
        //     // 设置滚动条类型
        //     type: "slider",
        //     // 数据窗口范围的起始数值
        //     startValue: 0,
        //     // 数据窗口范围的结束数值（一页显示多少条数据）
        //     endValue: 22,
        //     // 设置滚动条宽度，相对于盒子宽度
        //     width: "92%",
        //     // 设置滚动条高度
        //     height: 3,
        //     // 设置滚动条显示位置
        //     left: "8%",
        //     // dataZoom-slider组件离容器下侧的距离
        //     bottom: 0,
        //     // 设置选中范围的填充颜色
        //     fillerColor: "#449FED",
        //     // 设置边框颜色
        //     borderColor: "rgba(19, 120, 172, 1)",
        //     // 设置背景颜色
        //     backgroundColor: "#00F5FE",
        //     // 是否显示detail，即拖拽时候显示详细数值信息
        //     showDetail: false,
        //     //是否显示数据阴影 默认auto
        //     showDataShadow: false,
        //     brushSelect: false,
        //     // 是否锁定选择区域（或叫做数据窗口）的大小
        //     zoomLoxk: true,
        //     // empty：当前数据窗口外的数据，被设置为空，即不会影响其他轴的数据范围
        //     filterMode: "empty",
        //     // 控制手柄的尺寸
        //     handleSize: 0,
        //     moveHandleStyle: {
        //       opacity: 0,
        //     },
        //   },
        //   {
        //     // 没有下面这块的话，只能拖动滚动条，
        //     // 鼠标滚轮在区域内不能控制外部滚动条
        //     type: "inside",
        //     // 滚轮是否触发缩放
        //     zoomOnMouseWheel: false,
        //     // 鼠标滚轮触发滚动
        //     moveOnMouseMove: true,
        //     moveOnMouseWheel: true,
        //     startValue: 0,
        //     endValue: 22,
        //   },
        // ],

        xAxis: {
          type: "category",
          data: xData,
          splitLine: {show: false},
          axisTick: {show: false},
          splitArea: {show: false},
          axisLine: {
            lineStyle: {
              color: "rgba(176, 222, 248, 1)",
            },
          },
          axisLabel: {
            color: "#1D4A77",
            interval: 0,
            fontSize: "14px",
            align: "center",
            formatter: function (value) {
              return value.length > 4 ? value.slice(0, 4) + '...' : value; // 只显示前4个字符，并添加省略号
            }
          },
        },
        yAxis: [
          {
            name: "人",
            type: "value",
            splitNumber: yData1.every(value => value <= 1)?1:6,
            nameTextStyle: {
              color: "#1D4A77",
              fontSize: 14,
              align: "center",
              padding: [0, 30, 0, 0],
            },
            axisLabel: {
              formatter: "{value}",
              color: "rgba(95, 187, 235, 1)",
              textStyle: {
                fontSize: 14,
                color: "#1D4A77",
                lineHeight: 16,
              },
            },
            axisTick: {show: false},
            axisLine: {show: false},
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(176, 222, 248, 1)",
                type: "dashed",
              },
            },
          },
          {
            name: "%",
            type: "value",
            splitNumber: 1,
            nameTextStyle: {
              color: "#1D4A77",
              fontSize: 14,
              align: "center",
              padding: [0, -30, 0, 0],
            },
            axisTick: {show: false},
            splitLine: {show: false},
            axisLabel: {
              formatter: "{value}",
              color: "rgba(95, 187, 235, 1)",
              textStyle: {
                fontSize: 14,
                color: "#1D4A77",
                lineHeight: 16,
              },
            },
          }
        ],
        series: [
          {
            name: '代表人数',
            type: "bar",
            data: yData1,
            barWidth: 14,
            showBackground: true,
            backgroundStyle: {
              borderWidth: 1,
              borderColor: "rgba(152, 222, 223, 1)",
              color: "rgba(191, 229, 222, 1)",
            },
            itemStyle: {
              color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "rgba(49, 190, 116, 1)", },
                { offset: 1, color: "rgba(112, 216, 161, 1)", },
              ]),
              borderWidth: 1,
            },
          },
          {
            name: "",
            type: "scatter",
            data: yData1,
            emphasis: { scale: false },
            symbol: 'rect',
            symbolSize: [15, 3], // 进度条白点的大小
            itemStyle: {
              color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0,  color: "#00D4C8", },
                { offset: 1, color: "#00D4C8", },
              ], false),
              shadowColor: '#5DFFAB',
              shadowBlur: 1,
              borderWidth: 1,
              borderColor:'#5DFFAB',
              barBorderRadius: 0,
            },
            z: 2,
          },
          {
            name: "代表占比",
            type: "line",
            data: yData2,
            showSymbol: true,
            symbolSize: 8,
            yAxisIndex: 1,
            lineStyle: {
              color: "#F19149",
            },
            itemStyle: {
              normal: {
                color: 'rgba(241, 145, 73, 1)',
                borderColor: 'rgba(241, 145, 73, 0.3)', // 外圆描边色（空心）
                borderWidth: 8, // 外圆描边粗细
                lineStyle: {
                  color: 'rgba(241, 145, 73, .6)',
                  width: 2
                },
              }
            },
          },
        ],
      };
      myChart.setOption(option);
      window.addEventListener("resize", function () {
        myChart.resize();
      });
    }
  },
}
</script>

<style lang="scss" scoped>
.echartBar {
  width: 100%;
  height: 100%;
}
</style>